package com.ww7h.bigobj

import android.os.Bundle
import android.util.SparseArray
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.ww7h.bigobj.databinding.ActivityMainBinding
import com.ww7h.mvvm.ViewActivity
import com.ww7h.ui.base.ui.adapter.item.ItemSpace

class MainActivity : ViewActivity<ActivityMainBinding>() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        defaultDataBinding.demoListView.layoutManager = LinearLayoutManager(this)
        val adapter = DemoAdapter(this)
        defaultDataBinding.demoListView.adapter = adapter

        val dataList = ArrayList<DemoModel>()
        for (i in 0..20) {
            val data = DemoModel()
            data.title = "title $i"
            data.content = "content $i"
            when (i) {
                0 -> {
                    data.viewType = DemoViewHolderFactory.TYPE_HEAD
                }
                20 -> {
                    data.viewType = DemoViewHolderFactory.TYPE_FOOTER
                }
                else -> {
                    data.viewType = DemoViewHolderFactory.TYPE_CENTER
                }
            }

            dataList.add(data)
        }

        adapter.addAllDataList(dataList)
        adapter.addAllSpaceArray(SparseArray<ItemSpace>().apply {
            put(DemoViewHolderFactory.TYPE_HEAD, ItemSpace().apply {
                topDrawable = resources.getDrawable(R.drawable.ic_launcher_background, null)
                bottomDrawable = resources.getDrawable(R.drawable.ic_launcher_background2, null)
                rect.set(30, 100, 30, 100)
                topDrawableMargin.set(10, 0, 10, 0)
                bottomDrawableMargin.set(10, 0, 10, 0)
            })
            put(DemoViewHolderFactory.TYPE_CENTER, ItemSpace().apply {
                topDrawable = resources.getDrawable(R.drawable.ic_launcher_background, null)
                bottomDrawable = resources.getDrawable(R.drawable.ic_launcher_background2, null)
                rect.set(20, 10, 20, 10)
                topDrawableMargin.set(10, 0, 10, 0)
                bottomDrawableMargin.set(10, 0, 10, 0)
            })
            put(DemoViewHolderFactory.TYPE_FOOTER, ItemSpace().apply {
                topDrawable = resources.getDrawable(R.drawable.ic_launcher_background, null)
                bottomDrawable = resources.getDrawable(R.drawable.ic_launcher_background2, null)
                rect.set(10, 50, 10, 50)
                topDrawableMargin.set(10, 0, 10, 0)
                bottomDrawableMargin.set(10, 0, 10, 0)
            })
        })
    }

    override fun getContentViewLayoutId(): Int {
        return R.layout.activity_main
    }
}